#include "LoginDialog.h"
#include "ui_LoginDialog.h"
#include "Core.h"


LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);

    QRect frect = frameGeometry();
    frect.moveCenter(QDesktopWidget().availableGeometry().center());
    move(frect.topLeft());
}

LoginDialog::~LoginDialog()
{
    delete ui;
}
bool LoginDialog::CheckEmail(QString email)
{
    if(email.endsWith("@gmail.com"))
        return true;
    else
        return false;
}

void LoginDialog::on_Create_clicked()
{
    if(!ui->login->text().isEmpty() && !ui->pass->text().isEmpty() && CheckEmail(ui->login->text()))
    {
        set.setValueForKey(SettingsKeys::KEY_COUNT_ACCOUNT,set.getValueOfKey(SettingsKeys::KEY_COUNT_ACCOUNT).toInt()+1);
        set.EncryptPassword(ui->login->text(),ui->pass->text());
        set.setValueForKey(QString("Account/").append(ui->login->text()).append("/notifications"),true);
        emit CreateDatabase(ui->login->text());
        LoginDialog::accept();
    }
    else
    {

        ui->login->setStyleSheet("QLineEdit{border:1px solid red;}");
        ui->pass->setStyleSheet("QLineEdit{border:1px solid red;}");
        ui->labelLogError->setText("Check email or password");
    }
}

void LoginDialog::on_login_textChanged(const QString &arg1)
{
    ui->login->setStyleSheet("QLineEdit{border:1px solid gray;}");
    ui->pass->setStyleSheet("QLineEdit{border:1px solid gray;}");
    ui->labelLogError->setText("");
}

void LoginDialog::on_pass_textChanged(const QString &arg1)
{
    ui->login->setStyleSheet("QLineEdit{border:1px solid gray;}");
    ui->pass->setStyleSheet("QLineEdit{border:1px solid gray;}");
    ui->labelLogError->setText("");
}

